<template>
<div>
    <div class="pkg-step"></div>
    <div class="copy-area">
        <p>拷贝pkg资源文件</p>
        <el-input   class="source-path item" v-model="sourcePath" placeholder="源目录的绝对路径"></el-input>
        <el-input  class="target-path item" v-model="targetPath" placeholder="目标目录的绝对路径"></el-input>
        <div class="btn-list">
            <el-button  @click="startCopy"  type="primary" class="btn item">开始拷贝</el-button>
            <el-button  @click="startInit" type="danger" class="btn item">初始化</el-button>
        </div>
    </div>

    <div class="copy-area">
        <p>拷贝pkg打包文件</p>
        <el-input  class="source-path item" v-model="pkgSourcePath" placeholder="源目录的绝对路径"></el-input>
        <el-input  class="target-path item" v-model="pkgTargetPath" placeholder="目标目录的绝对路径"></el-input>
        <div class="btn-list">
            <el-button  @click="startCopy"  type="primary" class="btn item">开始拷贝</el-button>
            <el-button  @click="startInit" type="danger" class="btn item">初始化</el-button>
        </div>
    </div>
</div>
</template>

<script>
let SOURCE_PATH = "SOURCE_PATH";
let TARGET_PATH = "TARGET_PATH";
const fs = window.require('fs');
const exec = window.require('child_process').exec;
// const path = window.require("path");
var stat=fs.stat;
export default {
   name:"uniapp-pkg-assistant",
   data(){
       return {
           sourcePath:"/Users/mengze/Desktop/company_his_app/yunyihis_app/unpackage/resources/__UNI__020D012",
           targetPath: "/Users/mengze/Downloads/HBuilder-Integrate-AS-master/simpleDemo/src/main/assets/apps/__UNI__020D012",
           pkgSourcePath:"/Users/mengze/Downloads/HBuilder-Integrate-AS-master/simpleDemo/build/outputs/apk/debug/simpleDemo-debug.apk",
           pkgTargetPath:"/Users/mengze/Desktop/company_his_app/yunyihis_app/release/simpleDemo-debug.apk"

       }
   },
   methods: {
       /***
        * 开始拷贝资源文件
        */
       startCopy(){
           localStorage.setItem(SOURCE_PATH, this.sourcePath);
           localStorage.setItem(TARGET_PATH, this.targetPath);

           //开始拷贝文件
           this.exists(this.sourcePath, this.targetPath, this.copy);
       },

       /***
        * 获取上一次的资源文件路径
        */
       startInit(){
           this.sourcePath = localStorage.getItem(SOURCE_PATH) || "";
           this.targetPath = localStorage.getItem(TARGET_PATH) || "";

            console.log("执行命令：cd /Users/mengze/Desktop/company_his_app/yunyihis_app")
            exec( "npm run app", {
                cwd: '/Users/mengze/Desktop/company_his_app/yunyihis_app'
            }, ( err, stdout, stderr )=>{
                console.log(process.cwd())
                console.log("err", err);
                console.log("stdout", stdout);
                console.log("stderr", stderr)
            } )
            // exec("cd /Users/mengze/Desktop/company_his_app/yunyihis_app && npm run app", ( err, stdout, stderr )=>{
            //   
            //     // // console.log("url:",url)
            //     // exec("npm run app", (err, stdout, stderr)=>{  
            //     // console.log("err", err);
            //     // console.log("stdout", stdout);
            //     // console.log("stderr", stderr)
            //     // })
            // })
            

            
       },

        copy(src,dst) {
            //读取目录
            let that =this;
            fs.readdir(src,function(err,paths){
                console.log(paths)
                if(err){
                    throw err;
                }
                paths.forEach(function(path){
                    var _src=src+'/'+path;
                    var _dst=dst+'/'+path;
                    var readable;
                    var writable;
                    stat(_src,(err,st)=>{
                        if(err){
                            throw err;
                        }
                        
                        if(st.isFile()){
                            readable=fs.createReadStream(_src);//创建读取流
                            writable=fs.createWriteStream(_dst);//创建写入流
                            readable.pipe(writable);
                            console.log("开始拷贝文件")
                        }else if(st.isDirectory()){
                            that.exists(_src,_dst,that.copy);
                        }
                    });
                });
            });
        },

        exists(src,dst,callback){
            // console.log("exists")
            //测试某个路径下文件是否存在
            fs.exists(dst,function(exists){
                console.log( exists )
                if(exists){//存在
                    callback(src,dst);
                }else{//不存在
                    fs.mkdir(dst,function(){//创建目录
                        callback(src,dst)
                    })
                }
            })
        }

   }
}
</script>

<style lang="less" scoped>

.pkg-step {
    background-color: #f5f5f5;
}

.copy-area {

    margin: 40px;
    padding: 30px 40px;
    border: 1px solid #ddd;
    display: flex;
    flex-direction: column;
    p {
        margin-bottom: 15px;
        font-weight: bold;
        font-size: 18px;
    }
    .source-path ,.target-path {
        width: 500px;
    }

    .item {
        margin-bottom: 15px;
    }
    .btn-list {
        margin-top: 10px;
        display: flex;
        width: 500px;
        .btn {
            flex: 1;
        }
    }
}

</style>